<?php
namespace Admin\Controller;

class NewsCategoryController extends BaseController
{
    public function indexAction()
    {
        $NewsCategory = D('NewsCategory');
        $condition = array();
		$list = $NewsCategory->where($condition)->order('id asc')->select();
		$this->assign('list', $list);
        $this->display();
    }

    public function addAction()
    {
        if (IS_POST) {
            $NewsCategory = D('NewsCategory');
            $data = $NewsCategory->create($_POST['NewsCategory']);
            if (false !== $data) {
                $insertId = $NewsCategory->add();
                if (false !== $insertId) {
                    $this->success('新增成功', U('NewsCategory/index'));
                } else {
                    $this->error('新增失败（失败原因：' . $NewsCategory->getError() . '）');
                }
            } else {
                $this->error($NewsCategory->getError());
            }
        } else {
            $this->display();
        }
    }
    
    public function updateAction()
    {
        $NewsCategory = D('NewsCategory');
        if (IS_POST) {
            $data = $NewsCategory->create($_POST['NewsCategory']);
            if (false !== $data) {
                if (false !== $NewsCategory->save()) {
                    $this->success('更新成功', U('NewsCategory/index'));
                } else {
                    $this->error('更新失败（失败原因：' . $NewsCategory->getError() . '）');
                }
            } else {
                $this->error($NewsCategory->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $category_info = $NewsCategory->find($id);
            if (false === $category_info) {
                $this->error('获取信息失败！');
            }
            $this->assign('category_info', $category_info);
            $this->display();  
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $NewsCategory = M('NewsCategory');
        if (false !== $NewsCategory->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    public function sortAction() {
        $NewsCategory = D('NewsCategory');
        $arr_sort = I('post.sort');
        foreach ($arr_sort as $key => $val) {
            $data = array('sort' => intval($val));
            $NewsCategory->where('id = ' . $key)->save($data);
        }
        $this->redirect('NewsCategory/index');
    }


	/**
     * 编辑器上传处理
     */
	public function uploadJsonAction() 
    {
		$config = array(
			'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'newsCategory/',
            'subPath' => 'newsCategory/',
		);
        vendor('Kindeditor.UploadJson', '', '.class.php');
		$UploadJson = new \UploadJson($config);
		$UploadJson->upload();
	}
    
	/**
     * 编辑器上传文件管理
     */
	public function fileManagerJson()
    {
		
	}
}